<?php
namespace Api\Controller;
use Pingpp\Error\Base;
use Think\Controller;
use Think\Model;

class IndexController extends BaseController
{

    /**
     *师傅首页
     * @param $phone 手机号码
     * @param $key   登录凭证
     */
    public function masterIndex()
    {
        //解决跨域
        echoHeader();
        $phone = I('post.id','','int');

       /* $key = I('post.key', '');
        if(!verifyKey($phone,$key) || empty($key)){
            $data = array(err_code => 1003, err_msg => '登录超时');
            $this->ajaxReturn($data, 'JSON');
        }*/

        if(empty($phone)){
            $data=array('err_code'=>1001,'err_msg'=>'id不能为空');
            $this->ajaxReturn($data,'JSON');
        }


        $data = array();
        $where['user_id']=$phone;
        $user=M('user')->where($where)->select();
        if($user[0]['is_real']!=1){
            $data['is_real']=$user[0]['is_real'];
            if($user[0]['is_real']==0){
                $code=1004;
            }elseif($user[0]['is_real']==2){
                $code=1005;
            }else{
                $code=1006;
            }
            $data['renovation']=false;
            $data['repair']=false;
            $data['supply']=false;
            $data=array('err_code'=>$code,'err_msg'=>$data);  //未通过实名认证 || 实名认证中 || 审核不通过
            $this->ajaxReturn($data,'JSON');
        }

        if($user[0]['master_status']==1  || $user[0]['master_status']==3){
            $data['is_real']=$user[0]['is_real'];
            $data['renovation']=false;
            $data['repair']=false;
            $data['supply']=false;
            $data=array('err_code'=>1201,'err_msg'=>'用户被锁定，请联系管理员');
            $this->ajaxReturn($data,'JSON');
        }
        //查询维修配置
        $renovationWhere['user_id']=$user[0]['user_id'];
        $renov=M('user_renovation_conf')->where($renovationWhere)->select();

        $renovation=$renov?true:false;


        //查询装修配置
        $repairWhere['user_id']=$user[0]['user_id'];
        $repa=M('user_repair_conf')->where($repairWhere)->select();
        $repair=$repa?true:false;
        $repairWhere['mode_cate']=1;
        $mode=M('user_mode')->field('mode_order,mode_charging')->where($repairWhere)->select();
        $restr='';
        foreach($repa as $rk=>$rv){
               $restr.=$rv['category_id'].',';
        }

        session('repair_config',trim($restr,','));
        session('mode',$mode);
        $mode=session('mode');
      //  file_put_contents("/tmp/test.txt","mode:".var_export($mode,true)."----",FILE_APPEND);
        //查询供应商配置
        $supplyWhere['user_id']=$user[0]['user_id'];
        $supply=M('user_supply_commodity')->where($supplyWhere)->select()?true:false;
       /* if(empty($renovation) && empty($repair) && empty($supply)){
            $data['is_config']=false;
        }else{
            $data['is_config']=true;
        }*/
        //修改当前身份状态
        $map['identity']=2;

        //返回最新待服务的订单
        $status = '1,2,-3';
        $maps['worker_user_id'] = $user[0]['user_id'];
        $maps['status'] = array('in',$status);
        $order_id = M('repair_order')->field('repair_order_id')->where($maps)->select();
        $handleOrder = '';
        if($order_id) {
            $handleOrder = count($order_id);
        }
        $data['handleOrder'] = $handleOrder;

        if(M('user')->where($where)->save($map)===false){
            $data=array('err_code'=>403,'err_msg'=>false);
            $this->ajaxReturn($data,'JSON');
        }
        $data['is_real']=$user[0]['is_real'];
        $data['renovation']=$renovation;
        $data['repair']=$repair;
        $data['supply']=$supply;
        $data=array('err_code'=>200,'err_msg'=>$data);
        $this->ajaxReturn($data,'JSON');
    }



    //banner接口
    public function banner(){
        $where['status']=1;
        $bannerList=M('banner')->field('id,pic,title,desc,type')->where($where)->select();

        $data=array('err_code'=>200,'err_msg'=>$bannerList);
        $this->ajaxReturn($data,'JSON');
    }



    //业主首页
    public function ownerIndex(){
        //注释用户登录
        // $user_id = I('post.id', '','int');

        // if(empty($user_id)){
        //     $data=array('err_code'=>403,'err_msg'=>'缺少用户id');
        //     $this->ajaxReturn($data,'JSON');
        // }

        // $uwhere['user_id']=$user_id;
        // $user=M('user')->field('is_real,user_status')->where($uwhere)->find();

        // if(($user['user_status']==1  || $user['user_status']==3)){
        //     $data=array('err_code'=>2016,'err_msg'=>'用户被锁定，请联系管理员');
        //     $this->ajaxReturn($data,'JSON');
        // }

        // if(empty($user)){
        //     $user=false;
        // }else{
        //     $user=true;
        //     $where['user_id']=$user_id;
        //     $map['identity']=1;

        //     M('user')->where($where)->save($map);
        // }

        //返回待服务中的订单Id
        // $status = '1,2,-3';
        // $maps['user_id'] = $user_id;
        // $maps['status'] = array('in',$status);
        // $order_id = M('repair_order')->field('repair_order_id')->where($maps)->select();
        // $handleOrder = '';
        // if($order_id) {
        //     $handleOrder = count($order_id);
        // }
        $Classify = M('category')->where("status=1 and pid=0")->field('type,category_id,icon,category_name,desc')->order(array('order'=>'desc'))->select();
        $maintain=array();
        $fitment=array();
        if (!$Classify) {
            $Classify = array();
        }
        foreach($Classify as $k=>$v) {
            $second = M('category')->where("status=1 and type=0 and pid=" . $v['category_id'])->limit(3)->getField('category_name', true);
            $v['icon'] .= "?imageView2/1/w/100/h/100/q/100";
            if ($second) {
                $v['sonClassify'] = implode(',', $second);
            }else{
                $v['sonClassify'] = '';
            }
            /*      if($v['type']==0){
                      //维修
                      $maintain[]=$v;
                  }elseif($v['type']==1){
                      //装修
                      $fitment[]=$v;
                  }
                  unset($Classify[$k]);
              }*/
            /*$data['maintain']=$maintain;
            $data['fitment']=$fitment;*/
        }
    //    $Classify[]['handleOrder'] = $handleOrder;
        $data=array('err_code'=>200,'err_msg'=>$Classify);
        $this->ajaxReturn($data,'JSON');
    }

    /**
     * 是否能实名认证
     */

    public function identify(){
        $user_id = I('post.user_id');
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        if($data['user']['is_real']==3){
            $array = array('err_code'=>1004,'err_msg'=>'审核未通过');
        }elseif($data['user']['is_real']==2){
            $array = array('err_code'=>1005,'err_msg'=>'认证中');
        }elseif($data['user']['is_real']==1){
            $array = array('err_code'=>1006,'err_msg'=>'已实名');
        }elseif($data['user']['is_real']==0){
            $array = array('err_code'=>1003,'err_msg'=>'未实名认证');
        }
        $this->ajaxReturn($array);
    }

}